package cn.iocoder.yudao.module.member.alliance.controller.app;

import cn.iocoder.yudao.framework.business.basic.convert.BigDecimalConvert;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.infrastructure.config.ConfigApiV2;
import cn.iocoder.yudao.module.api.infrastructure.config.dto.MemberConfigRespDTO;
import cn.iocoder.yudao.module.member.alliance.dal.dataobject.CloudAllianceUserDO;
import cn.iocoder.yudao.module.member.alliance.service.AppCloudAllianceUserService;
import cn.iocoder.yudao.module.member.alliance.vo.AppAllianceUserBonusOverviewRespVO;
import cn.iocoder.yudao.module.member.alliance.vo.AppCloudAllianceCenterRespVO;
import cn.iocoder.yudao.module.member.alliance.vo.AppCloudAllianceUserVO;
import cn.iocoder.yudao.module.member.assets.convert.MemberDigitalAssetsConvert;
import cn.iocoder.yudao.module.member.assets.dal.dataobject.MemberDigitalAssetsDO;
import cn.iocoder.yudao.module.member.assets.service.AppMemberDigitalAssetsService;
import cn.iocoder.yudao.module.member.user.dal.dataobject.MemberUserDO;
import cn.iocoder.yudao.module.member.user.service.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.api.infrastructure.ConfigConstants.CONFIG_MEMBER;

@Tag(name = "用户 App - 云盟商信息")
@RestController
@RequestMapping("/member/cloud-alliance-user")
@Validated
public class AppCloudAllianceUserController {

    @Resource
    private AppCloudAllianceUserService appCloudAllianceUserService;
    @Resource
    private MemberUserService memberUserService;
    @Resource
    private AppMemberDigitalAssetsService appMemberDigitalAssetsService;

    @Resource
    private ConfigApiV2 configApiV2;

    @GetMapping("/center")
    @Operation(summary = "获得我的云盟中心数据")
    public CommonResult<AppCloudAllianceCenterRespVO> getMyCenter() {
        CloudAllianceUserDO aDo = appCloudAllianceUserService.getOrCreate(getLoginUserId());
        MemberDigitalAssetsDO digitalAssetsDO = appMemberDigitalAssetsService.getOrCreate(getLoginUserId());
        AppCloudAllianceCenterRespVO centerRespVO = new AppCloudAllianceCenterRespVO();
        centerRespVO.setUser(BeanUtils.toBean(aDo, AppCloudAllianceUserVO.class));
        centerRespVO.setDigitalAssets(MemberDigitalAssetsConvert.INSTANCE.convert(digitalAssetsDO));
        MemberConfigRespDTO memberConfigRespDTO = configApiV2.getConfig(CONFIG_MEMBER, MemberConfigRespDTO.class);
        AppAllianceUserBonusOverviewRespVO respVO = new AppAllianceUserBonusOverviewRespVO();
        respVO.setBonus(BigDecimalConvert.INSTANCE.convert(digitalAssetsDO.getBonus()));
        respVO.setBonusGet(BigDecimalConvert.INSTANCE.convert(digitalAssetsDO.getBonusGet()));
        respVO.setBonusToMoneyTips(memberConfigRespDTO.getBonusToMoneyTips());
        respVO.setBonusToMoneyPercent(memberConfigRespDTO.getBonusToMoneyPercent());
        centerRespVO.setBonus(respVO);
        return success(centerRespVO);
    }

    @GetMapping("/my")
    @Operation(summary = "获得我的身份详情")
    public CommonResult<AppCloudAllianceUserVO> getMyDetail() {
        CloudAllianceUserDO aDo = appCloudAllianceUserService.getOrCreate(getLoginUserId());
        AppCloudAllianceUserVO respVO = BeanUtils.toBean(aDo, AppCloudAllianceUserVO.class);
        return success(respVO);
    }

    @GetMapping("/get-user-simple")
    @Operation(summary = "获得用户简单信息")
    @Parameter(name = "cloudAllianceId", description = "云盟商编号", required = true, example = "1024")
    @PermitAll
    public CommonResult<UserSimpleVO> getUserSimple(@RequestParam("cloudAllianceId") Long cloudAllianceId) {
        AppCloudAllianceUserVO respVO = appCloudAllianceUserService.getDetail(cloudAllianceId);
        if (respVO == null || respVO.getUserId() == null || respVO.getUserId() <= 0) {
            return success(null);
        }
        MemberUserDO user = memberUserService.getUser(respVO.getUserId());
        return success(BeanUtils.toBean(user, UserSimpleVO.class));
    }

}